﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using System.IO;
using System.Web.Security;
using System.Data;
using System.Text.RegularExpressions;
using System.Net;

namespace University.Workshop.WebApp
{
	public partial class ShoppingCart : System.Web.UI.Page
	{
        
        Collection<Product> list;
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            if (!IsPostBack)
            {
                list = FillData();
                if (list != null)
                {
                    DataList1.DataSource = list;
                    DataList1.DataBind();
                    Session["carrito"] = list;
                }
                else
                    this.MultiView1.ActiveViewIndex = 1;
            }
		}
        protected Collection<Product> FillData()
        {
                list = Session["carrito"] as Collection<Product>;
                Product prod = Session["products"] as Product;
                if (prod != null)
                {
                    Session.Remove("products");
                    long id = prod.ProductId;
                    short quantity = prod.Quantity;
                    list = ShoppingCartManager.AddToTheCart(id,quantity);
                    if (list != null)
                    {
                        double totalPrice = 0;
                        foreach (Product p in list)
                        {
                            double price = p.Price;
                            if (p.Currency == "Bolivianos")
                            {
                                price = System.Convert.ToDouble(Conver(System.Convert.ToDecimal(price), "BOB", "USD"));
                                price = Math.Round(price, 2);
                            }
                            totalPrice += price * p.Quantity;
                        }
                        totalPricelb.Text = totalPrice.ToString();
                    }
                    list = GetProductList(list);
                    totalCurrencylb.Text = "$us";
            }
            return list;
 
        }
        protected void ContinueShopping_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewProduct.aspx");
            
        }
        protected void ProcededToCheckout_Click(object sender, EventArgs e)
        {
            Response.Redirect("Checkout.aspx");
        }
        protected Collection<Product> GetProductList(Collection<Product> list)
        {
            return list;
        }
        private decimal Conver(decimal amount, string fromCurrency, string toCurrency)
        {
            WebClient web = new WebClient();

            string url = string.Format("http://www.google.com/ig/calculator?hl=en&q= {2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);

            string response = web.DownloadString(url);

            Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
            Match match = regex.Match(response);

            decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);

            return rate;
        }

        protected void Delete_Click1(object sender, EventArgs e)
        {
            try
            {
                    Button b = sender as Button;
                    long productId = Convert.ToInt64(b.CommandArgument);
                    list = FillData();
                    list = ShoppingCartManager.RemoveAProduct(productId, list);
                    if (list.Count < 1)
                    {
                        this.MultiView1.ActiveViewIndex = 1; 
                    }
                    if (list != null)
                    {
                        DataList1.DataSource = list;
                        DataList1.DataBind(); 
                        double totalPrice = 0;
                        foreach (Product p in list)
                        {
                            double price = p.Price;
                            if (p.Currency == "Bolivianos")
                            {
                                price = System.Convert.ToDouble(Conver(System.Convert.ToDecimal(price), "BOB", "USD"));
                                price = Math.Round(price, 2);
                            }
                            totalPrice += price * p.Quantity;
                        }
                        totalPricelb.Text = totalPrice.ToString();

                        Session["carrito"] = list;
                    }
            }
            catch (Exception ex)
            {
                errorLb.Text = ex.Message;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewProduct.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("WatchList.aspx");
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            
        }


	}

}
